﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Collections;
using System.IO;

namespace nLearning
{
    /// <summary>
    /// Interaction logic for MainView.xaml
    /// </summary>
    public partial class MainView : Window
    {
        public MainView()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           //BatchMapDrive();
            Zyg40.Database.ConnectServer.ConnectionString = Query.SQLConnectionString("202.57.128.228", "sa", "nextwavermay", "Business_Knowledge");
            //Zyg40.Database.ConnectServer.ConnectionString = Query.SQLConnectionString("ANGOLOUS-PC\\SQL2K8EXP", "sa", "nextwaver", "Business_Knowledge");
            //Zyg40.Database.ConnectServer.ConnectionString = Query.SQLConnectionString("122.155.190.7", "jigsaw", "TEERAman659", "DB_Jigsaw");

            UC.Login log = new UC.Login();
            ShowControl(log);
        }

        public void ShowControl(UserControl uc)
        {
            pageTransitionControl.ShowPage(uc);
        }

        #region network drive control
        public static bool IsDriveMapped(string sDriveLetter)
        {
            sDriveLetter = sDriveLetter.ToUpper();
            string[] DriveList = Environment.GetLogicalDrives();
            for (int i = 0; i < DriveList.Length; i++)
            {
                if (sDriveLetter + ":\\" == DriveList[i].ToString())
                {
                    return true;
                }
            }
            return false;
        }

        private void BatchUnmapDrive()
        {
            string sectionName = "ShareLink";
            string conf = System.Environment.CurrentDirectory + "\\Config\\Configurations.xml";

            DataTable dtLink = new DataTable();
            dtLink = Zyg40.StandardDoc.ConvertToDataTable(conf, sectionName);

            for (int i = 0; i < dtLink.Rows.Count; i++)
            {
                try
                {
                    string link = dtLink.Rows[i]["ShareLink"].ToString();
                    string point = dtLink.Rows[i]["MapPoint"].ToString();
                    string tmp = point.Replace(":", "");

                    if (IsDriveMapped(tmp))
                    {
                        aejw.Network.NetworkDrive oNetDrive = new aejw.Network.NetworkDrive();
                        //Map drive with specified user credentials
                        oNetDrive.LocalDrive = point;
                        oNetDrive.ShareName = link;
                        oNetDrive.Force = true;
                        oNetDrive.UnMapDrive();
                    }
                }
                catch
                {
                    //do something
                }
            }
        }

        private void BatchMapDrive()
        {
            string sectionName = "ShareLink";
            string conf = System.Environment.CurrentDirectory + "\\Config\\Configurations.xml";

            ///*
            ///*by Likhit Choompoopati
            ///*Add some code to store what drive is already map.
            ///*

            DataTable dtLink = new DataTable();
            dtLink = Zyg40.StandardDoc.ConvertToDataTable(conf, sectionName);
            ArrayList driveList = new ArrayList();

            for (int i = 0; i < dtLink.Rows.Count; i++)
            {
                try
                {
                    string link = dtLink.Rows[i]["ShareLink"].ToString();
                    string user = dtLink.Rows[i]["UserName"].ToString();
                    string pass = dtLink.Rows[i]["Password"].ToString();
                    string point = dtLink.Rows[i]["MapPoint"].ToString();
                    string tmp = point.Replace(":", "");
                    if (!IsDriveMapped(tmp))
                    {
                        try
                        {
                            aejw.Network.NetworkDrive oNetDrive = new aejw.Network.NetworkDrive();
                            //oNetDrive.Force = true;
                            oNetDrive.LocalDrive = point;
                            oNetDrive.ShareName = link;
                            oNetDrive.MapDrive(user, pass);
                        }
                        catch { }
                    }
                    if (IsDriveMapped(tmp))
                    {
                        driveList.Add(point);
                        Config.Configurations.BranchID = dtLink.Rows[i]["Branch"].ToString();
                    }
                }
                catch (Exception Ex)
                {
                    Console.WriteLine(Ex.Message);
                }
            }
            Warehouse.NetworkDrive = driveList;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //BatchUnmapDrive();
        }
        #endregion
    }
}
